﻿using Microsoft.EntityFrameworkCore;

namespace myDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            //获取数据库连接字符串
            var sqlConnectionString = Configuration.GetConnectionString("DefaultConnection");

            //添加数据上下文
            services.AddDbContext<MyContext>(options =>
                options.UseNpgsql(sqlConnectionString)
            );
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MyContext myContext)
        {
            myContext.Database.Migrate();
            if (!myContext.students.Any())
            {
                myContext.students.Add(new Student
                {
                    FirstName = "Dave",
                    LastName = "Carter",
                    Gender = Gender.男

                });
                myContext.students.Add(new Student
                {
                    FirstName = "Mary",
                    LastName = "Johnson",
                    Gender = Gender.女

                });

                myContext.SaveChanges();
            }

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}
